package com.xyebank.ledger.service.zl.adapter.jry;

import org.modelmapper.ModelMapper;
import org.modelmapper.PropertyMap;
import org.springframework.stereotype.Component;

import com.xyebank.ledger.db.entitys.LApplicationInf;
import com.xyebank.ledger.service.common.conveter.ApplyBeanConverter;
import com.xyebank.ledger.service.constant.CommonConstant;
import com.xyebank.ledger.service.constant.ProductConstant;
import com.xyebank.ledger.util.DateConverter;
import com.xyebank.ledger.util.IDUtil;
import com.xyebank.ledger.util.StatusConvertUtil;
import com.xyebank.ledger.vo.deal.ZLJRYApplicationInfVo;

@Component(value = "zlJryApplyBeanConverter")
public class ZLApplyBeanConverter implements ApplyBeanConverter<ZLJRYApplicationInfVo> {
    
    // 不能采取注入方式
    private DateConverter dateConverter = new DateConverter();
  
    
    ModelMapper modelMapper = new ModelMapper();


    class ZLJRYApplyPropertyMap extends PropertyMap<ZLJRYApplicationInfVo, LApplicationInf> {
        protected void configure() { //注意　不要有重复的映射，不然会报错
            map(source.getApplyNo(), destination.getXyOrderNo());
            map(source.getMblNo(), destination.getMblNo());
            using(dateConverter).map(source.getApplyTime(), destination.getApplyTime()); //　日期格式转换错误
            map(source.getCreditLimit(), destination.getCreditAmount());
            map(source.getResultDesc(), destination.getRefuseDesc());
       }
    }

    public ZLApplyBeanConverter() {
        modelMapper.addMappings(new ZLJRYApplyPropertyMap());
    }

    @Override
    public LApplicationInf covert(ZLJRYApplicationInfVo x) {
        LApplicationInf entity = modelMapper.map(x, LApplicationInf.class);
        
        entity.setId(IDUtil.generateId(x.getId().toString(), "zl_jry"));
        entity.setDataSource(CommonConstant.JRY_STR);
        entity.setXyOrgNo(ProductConstant.ZL_LOAN_ORG);
        entity.setStatus(StatusConvertUtil.convertApplyStatus(x.getApplyStatus()));
        buildXyProdNo(entity, x.getProductCode());
        return entity;
    }

    private void buildXyProdNo(LApplicationInf pEntity, String pProductCode) {
        switch(pProductCode){
            case "XJXE001":
                pEntity.setXyProductNo(ProductConstant.ZL_LOAN_SJSH_PROD_ID);
                break;
            case "XJBL001":
                pEntity.setXyProductNo(ProductConstant.ZL_LOAN_BLD_PROD_ID);
                break;
            case "XJGJJ01":
                pEntity.setXyProductNo(ProductConstant.ZL_LOAN_GJJD_PROD_ID);
                break;
            case "XJDQD01":
                pEntity.setXyProductNo(ProductConstant.ZL_LOAN_DQD_PROD_ID);
                break;
            case "XJYZ001":
                pEntity.setXyProductNo(ProductConstant.ZL_LOAN_YZD_PROD_ID);
                break;
            default:
                
                break;
        }
                
        
    }

}
